package com.easy.controller;


import com.easy.bean.User;
import com.easy.service.IShoppingLoginService;
import com.easy.util.LayData;
import com.easy.util.Page;
import com.easy.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    IShoppingLoginService userService;


    @RequestMapping("list")
    @ResponseBody
    public LayData getList(Page page, String text, HttpSession session) {
        User user = (User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        List<User> list = userService.getList(page, text, user.getId());
        int count = userService.getCount(text, user.getId());
        System.out.println(count);
        return new LayData(count,list);
    }



    @RequestMapping("list1")
    @ResponseBody
    public LayData getAdminList(Page page, String text) {
        // 管理员查看所有用户信息，不需要用户ID限制
        List<User> list = userService.getList1(page, text);
        int count = userService.getCount1(text);
        return new LayData(count, list);
    }


    @RequestMapping("delete")
    @ResponseBody
    public int delete(int id){
        return userService.delete(id);
    }

    @RequestMapping("add")
    @ResponseBody
    public int add(User user){
        return userService.add(user);
    }

    @RequestMapping("edit")
    @ResponseBody
    public int edit(User user){
        return userService.edit(user);
    }

    @RequestMapping("getuserbyusername")
    @ResponseBody
    public LayData getUserByUsername(User user){
        User userData=userService.getUser(user);
        return LayData.success(userData);
    }

    @RequestMapping("register")
    @ResponseBody
    public LayData register(User user,String comfirmpassword){
        //验证格式是否正确
        boolean check=SYS.check("^[a-zA-Z0-9]{6,16}$",user.getUsername(),user.getPassword(),comfirmpassword);
        if(!check){
            return LayData.success(400,"输入的内容格式不正确");
        }
        if(!comfirmpassword.equals(user.getPassword())){
            return LayData.success(400,"两次输入的密码不一样");
        }

        User userData=userService.getUser(user);
        if(userData!=null){
            return LayData.success(400,"账号已存在");
        }
        String hexpass=SYS.digest(user.getPassword());
        user.setPassword(hexpass);
        int count=userService.add(user);
        return LayData.success(200,"注册成功",userData);
    }


    @RequestMapping("changePassword")
    @ResponseBody
    public LayData changePassword(String oldPassword, String newPassword, HttpSession session) {
        // 获取当前登录用户
        User currentUser = (User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if(currentUser == null) {
            return LayData.success(401, "用户未登录");
        }

        // 验证旧密码是否正确
        User dbUser = userService.getUserById(currentUser.getId());
        if(dbUser == null) {
            return LayData.success(404, "用户不存在");
        }

        // 验证旧密码
        if(!SYS.checkPass(oldPassword, dbUser.getPassword())) {
            return LayData.success(400, "当前密码不正确");
        }

        // 验证新密码格式
        if(!SYS.check("^[a-zA-Z0-9]{6,16}$", newPassword)) {
            return LayData.success(400, "新密码格式不正确，请使用6-16位字母或数字");
        }

        // 加密新密码
        String encryptedPassword = SYS.digest(newPassword);

        // 更新密码
        User updateUser = new User();
        updateUser.setId(currentUser.getId());
        updateUser.setPassword(encryptedPassword);

        int result = userService.updatePassword(updateUser);
        if(result > 0) {
            // 更新session中的用户信息
            currentUser.setPassword(encryptedPassword);
            session.setAttribute(SYS.SHOPPING_LOGIN_USER, currentUser);
            return LayData.success("密码修改成功");
        } else {
            return LayData.success(500, "密码修改失败，请稍后重试");
        }
    }


    @RequestMapping("verifyPassword")
    @ResponseBody
    public LayData verifyPassword(String password, HttpSession session) {
        // 获取当前登录用户
        User currentUser = (User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if(currentUser == null) {
            return LayData.success(401, "用户未登录");
        }

        // 获取数据库中的用户信息
        User dbUser = userService.getUserById(currentUser.getId());
        if(dbUser == null) {
            return LayData.success(404, "用户不存在");
        }

        // 验证密码
        if(SYS.checkPass(password, dbUser.getPassword())) {
            return LayData.success("密码验证成功");
        } else {
            return LayData.success(400, "当前密码不正确");
        }
    }

}

